

package yf.mall.service.impl;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yf.mall.dao.MallGoodsDao;
import yf.mall.dao.OrderItemDao;
import yf.mall.dao.UserDao;
import yf.mall.entity.UserEntity;
import yf.mall.enums.CommentStatusEnum;
import yf.mall.enums.OrderItemStatusEnum;
import  yf.mall.vo.down.CommentDown;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;
import yf.mall.vo.down.CommentDown;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;
import yf.mall.dao.CommentDao;
import yf.mall.entity.CommentEntity;
import yf.mall.vo.CommentVo;
import yf.mall.service.CommentService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Sep 15 10:27:09 CST 2020
 */
@Service
public class CommentServiceImpl extends BaseServiceImpl<CommentEntity> implements CommentService {

    private CommentDao dao;

    @Autowired
    private UserDao userDao;
    @Autowired
    private MallGoodsDao mallGoodsDao;
    @Autowired
    private OrderItemDao orderItemDao;

    @Autowired
    public void setDao(CommentDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }




    @Override
    public List<CommentVo> findVo(CommentVo vo){
        List<CommentVo> list = dao.findVo(vo);
        if (list != null && list.size() > 0){
            for (CommentVo commentVo : list) {
                if (commentVo.getEntity().getUserId() != null){
                    UserEntity userEntity = userDao.get(commentVo.getEntity().getUserId());
                    commentVo.getEntity().setUserCode(userEntity.getCode());
                }
                commentVo.getEntity().setGoodsName(mallGoodsDao.get(commentVo.getEntity().getGoodsId()).getName());
            }
        }
        return list;
    }

    @Override
    public CommentVo getVo(Long id){
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(CommentVo vo){
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(CommentVo vo){
        return dao.update(vo.getEntity());
    }



    @Override
    public void downLoad(HttpServletResponse response, List<CommentVo> list) {
        try {
            List<CommentDown> datas = list.stream().map(item -> new CommentDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel" );
            response.setCharacterEncoding("utf-8" );
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8" );
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
            EasyExcel.write(response.getOutputStream(), CommentDown.class).sheet("模板" ).doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 评论
     * @param userId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void comment(Long userId,Long goodsId,Long itemId,String content) {
        //初始化comment信息
        CommentEntity commentEntity = new CommentEntity();
        UserEntity userEntity = userDao.get(userId);
        commentEntity.setUserId(userId); //用户id
        commentEntity.setAvatar(userEntity.getAvatar());  //头像
        commentEntity.setName(userEntity.getName()); //用户名
        commentEntity.setTime(new Date()); //时间
        commentEntity.setStatus(CommentStatusEnum.AUDIT.getIndex()); //状态
        commentEntity.setGoodsId(goodsId); //产品id
        commentEntity.setItemId(itemId); //itemid
        commentEntity.setContent(content); // 内容
        //修改item评论状态
        orderItemDao.updateStatus(OrderItemStatusEnum.YES.getIndex(),commentEntity.getItemId());
        dao.save(commentEntity);
    }

    @Override
    public List<CommentEntity> findByGoodsId(Long goodsId) {
        return dao.findByGoodsId(goodsId);
    }


    /**内部方法**/



    /**外部方法实现**/




}
